Overview
Clerk JWT authentication enables multi-user authentication with industry-standard JWT tokens. This mode is recommended for:- Production deployments with multiple users
- Team collaboration environments
- Applications requiring user management features
Configuration
Prerequisites
Create Clerk Account
Sign up for a Clerk account at clerk.com
Backend Configuration
Set the following environment variables in your backend.env file:
backend/.env
Must be set to
clerk to enable Clerk JWT authenticationYour Clerk secret key (starts with
sk_live_ or sk_test_). Found in Clerk dashboard under API Keys.Clerk API base URL. Use default unless using Clerk’s EU instance.
Whether to verify the JWT
iat (issued at) claimClock skew tolerance in seconds for JWT validation. Allows for small time differences between servers.
Frontend Configuration
Set the following environment variables in your frontend.env.local file:
frontend/.env.local
Must be set to
clerkYour Clerk publishable key (starts with
pk_live_ or pk_test_). Found in Clerk dashboard under API Keys.Where to redirect users after successful sign in
Where to redirect users after signing out
Authentication Flow
User Signs In via Clerk
User authenticates through Clerk’s UI components (sign in modal, redirect flow, etc.)
Frontend Includes JWT
Frontend automatically includes the JWT in API requests via
Authorization: Bearer <jwt> headerBackend Verifies JWT
Backend verifies JWT signature using Clerk’s public keys and validates claims:
- Signature verification
- Expiration check (
expclaim) - Issued-at validation (
iatclaim with leeway) - Token type validation (must be
session_token)
JWT Token Structure
Required Claims
Mission Control validates the following JWT claims:Clerk user ID (e.g.,
user_2abcXYZ). Used as the primary user identifier.Token expiration timestamp (Unix epoch). Tokens are rejected after this time.
Token issued-at timestamp (Unix epoch). Validated with
CLERK_LEEWAY tolerance.Optional Claims (User Profile)
Mission Control extracts user profile information from these claims:Primary email address. Also checks
email_address and primary_email_address.Array of email address objects. Used as fallback if
email claim is not present.Full display name. Also checks
full_name.First name. Combined with
last_name if name is not present.Last name. Combined with
first_name if name is not present.API Usage
Making Authenticated Requests
Include the Clerk session token in theAuthorization header:
Bootstrap User Context
User Synchronization
Automatic User Creation
When a user authenticates for the first time, Mission Control:- Extracts
subclaim from JWT (Clerk user ID) - Looks up user by
clerk_user_idin the database - If not found, creates a new user record with:
- Email and name from token claims
- If claims are incomplete, fetches full profile from Clerk API
- Creates organization membership
Profile Updates
Mission Control updates user profiles on each authentication:- Email: Updated if changed in Clerk
- Name: Updated only if currently empty in database
- Other fields: Not automatically updated (user must update via API)
Clerk API Fallback
If email or name are missing from JWT claims, Mission Control fetches the full user profile from Clerk API using the/v1/users/{user_id} endpoint. This happens:
- On first authentication (user creation)
- When existing user has missing email or name
JWT Verification Details
Signature Verification
The backend uses the official Clerk SDK to verify JWT signatures:- Fetch public keys from Clerk’s JWKS endpoint
- Verify signature using RSA public key
- Check token type (must be
session_token) - Validate expiration with clock skew tolerance
Clock Skew Tolerance
TheCLERK_LEEWAY setting allows for small time differences between servers:
Error Handling
Missing Token
Request:401 Unauthorized
Invalid JWT Signature
Request:401 Unauthorized
Expired Token
Request with expired JWT Response:401 Unauthorized
Clerk automatically refreshes session tokens. If you receive 401 errors, ensure your client is using the latest token from
getToken().Missing User ID
JWT withoutsub claim
Response: 401 Unauthorized
User Deletion
When a user is deleted in Mission Control, the system can optionally delete the user from Clerk:DELETE /v1/users/{user_id}.
Common Issues
CORS Errors
Wrong Clerk Instance
Clock Skew Too Large
Missing Email or Name
Best Practices
Use environment-specific keys: Separate
test and live keys for development and productionRotate secret keys: Periodically rotate Clerk secret keys via the dashboard
Monitor token expiration: Clerk tokens typically expire after 60 minutes. Ensure your client refreshes tokens automatically.
Enable MFA: Configure multi-factor authentication in Clerk for enhanced security
Advanced Configuration
Custom JWT Claims
You can add custom claims to Clerk JWTs via Clerk’s dashboard or API. Mission Control will preserve these claims in thepayload dict but doesn’t use them by default.
Session Management
Clerk handles session management automatically:- Token refresh: Clerk SDK automatically refreshes tokens before expiration
- Session duration: Configure in Clerk dashboard (default: 7 days)
- Idle timeout: Configure automatic sign out after inactivity
Webhook Integration
You can configure Clerk webhooks to sync user changes in real-time:- Configure webhook URL in Clerk dashboard
- Subscribe to
user.updatedanduser.deletedevents - Implement webhook handler to update Mission Control database
Next Steps
Local Token
Simpler authentication for self-hosted setups
Agent Tokens
Learn about agent authentication
Clerk Documentation
Official Clerk documentation